<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2023 yoncron All rights reserved.
// +----------------------------------------------------------------------
// | Author: yoncron <yoncron@163.com>
// +----------------------------------------------------------------------
namespace app\middleware;


use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;

class CORS implements MiddlewareInterface{
    public function process(Request $request, callable $next) : Response
    {
        $response = $request->method() == 'OPTIONS' ? response('',200,['Content-Type' => 'application/json','Server' => 'X-Server']) : $next($request);
        $response->withHeaders([
            'Access-Control-Allow-Credentials' => true,
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET,POST,OPTIONS',
            'Access-Control-Allow-Headers' => 'appid,authorization,content-type,sign',
        ]);
        return $response;
    }
}